import * as echarts from '../../ec-canvas/echarts';

var uriUtil = require('../../utils/uri.js');

Page({
  data: {
    data_arr:["日","一","二","三","四","五","六"],
    year:0,
    month:0,
    today:0,
    num:0,
    ornot:false,//今天是否签到
    nowmonth:0,
    ec: {},
    calorie: [],
    days: ['6.3','6.4','6.5','6.6','6.7','6.8','6.9']
  },

  //签到
  qiandao(){ 
    let mday =uriUtil.getisDate()
    let nowtime = new Date().getDate()+new Date().getMonth()+1
    let num = uriUtil.getNum()
    let name = uriUtil.getUserInfo().username
    if( mday!=nowtime ) {
    wx.showToast({
      title: '打卡成功',
      duration:2000
    })
    num++
    this.setData({
      ornot:true,
      num:num,
    })
    uriUtil.setDate(nowtime)
    uriUtil.setUserNum(num)
    wx.request({
      url: uriUtil.BASE_URL+uriUtil.userJianchi,
      header:uriUtil.header,
      method:'POST',
      data:{
          name:name,
          days:num
      },
    })
    }
    else{
      wx.showToast({
        title: '今日已打卡',
        icon:'error',
        duration:2000
      })
    }
  },
  
  onShow: function (options) {  
   this.sevenDay() 
   let date = uriUtil.getisDate()//上次签到日期
   let now =  new Date()
   let today = now.getDate()
   let year = now.getFullYear()
   let ornot = false
   let number = uriUtil.getNum()
    // month获取是从 0~11
   let month = now.getMonth() + 1
   let newtoday = today+month
    if(date == newtoday){ornot=true}
    else{ornot = false}
    this.setData({
      ornot:ornot,
      year:year,
      today:newtoday,
      nowmonth:month,
      month:month,
      num:number,
      ec: {
        onInit: this.initChart
      }
    })
    this.showCalendar()
  },

  //获得七日卡路里
  sevenDay:function(){
    let that = this
    let user = uriUtil.getUserInfo()
    if(user){
      wx.request({
        url: uriUtil.BASE_URL+uriUtil.userCalorieList,
        method:"get",
        data:{
          uid:uriUtil.getUserInfo().id
        },
        success:function(res){
          that.setData({
            calorie:res.data
          })
        }
      })
    }
   
  },
  //日历渲染  
  showCalendar(){
    let {year,month} = this.data
    let currentMonthDays = new Date(year,month,0).getDate() //获取当前月份的天数
    let startWeek = new Date(year + '/' + month + '/' + 1).getDay(); //本月第一天是从星期几开始的
    this.setData({
      currentMonthDays,startWeek
    })
  },
  //上个月按钮
  bindPreMonth(){
    let {year,month} = this.data
    //判断是否是1月
    if(month - 1 >= 1){
      month = month - 1 
    }else{
      month = 12
      year = year - 1
    }
    this.setData({
      month,year
    })
    this.showCalendar()
  },
  //下个月按钮
  bindNextMonth(){
    let {year,month} = this.data
    //判断是否是12月
    if(month + 1 <= 12){
      month = month + 1 
    }else{
      month = 1
      year = year + 1
    }
    this.setData({
      month,year
    })
    this.showCalendar()
  },
 //曲线图加载
  initChart(canvas, width, height, dpr) {
    let that = this
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height,
      devicePixelRatio: dpr // 像素
    });
    canvas.setChart(chart);
    var option = {
      grid: {
        x: 15,
        y: 65,
      },
      tooltip: {
        show: true,
        trigger: 'axis',
        backgroundColor: 'rgba(255,255,255,0.8)',
        formatter: '{b0}日: {c0}kg',
        confine: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: that.data.days,
      },
      yAxis: {
        type: 'value',
        splitLine: {
          lineStyle: {
            type: 'dashed'
          }
        },
        show: false
      },
      series: [{
        name: 'A',
        type: 'line',
        smooth: true,
        data: that.data.calorie
      }]
    };
    chart.setOption(option);
    return chart;
  },
})